package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.entity.Blog;
import org.top10.entity.CollectEntity;
import org.top10.entity.Emotion;
import org.top10.entity.Ent;
import org.top10.entity.Finance;
import org.top10.entity.News;
import org.top10.entity.Reading;
import org.top10.entity.SinaBlogJsonData;
import org.top10.entity.SinaJsonData;
import org.top10.entity.Sport;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

abstract class SinaParser extends TOPParserImpl {

	public SinaParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract CollectEntity build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..
	
//	private CollectEntity createCE(int type){
//		switch(type){
//		case TOP10Global.TYPE_News:return new News();
//		case TOP10Global.TYPE_ENT:return new Ent();
//		case TOP10Global.TYPE_Finance:return new Finance();
//		}
//		return new Sport();
//	}
	protected List sinaBuild(ParseSource doc,int type) throws Exception{
			int i=doc.getTextSource().indexOf("\"data\":");
			List<SinaJsonData> SinaDatas=turnJson2List(doc.getTextSource().substring(i+7),SinaJsonData.class);
			List list = new ArrayList();
			for(SinaJsonData d:SinaDatas){
				CollectEntity news = TOP10Global.getCollectEntityByType(type);
				news.setName(d.getTitle());
				news.setLinkUrl(d.getUrl());
				//news.setRank(Integer.parseInt(d.getTop_num()));
				System.out.println(news);
				list.add(news);
			}
			return list;
	}
}



class NewsAllParser extends SinaParser{
	public NewsAllParser(String c, int type, String u) {
		super(c,type, u);
	}
	public CollectEntity build(Node node, int rank) throws Exception {
		return null;
	}
	public List parse(ParseSource doc) throws Exception {
		return sinaBuild(doc,type);
	}
}
class SinaBookParser extends TOPParserImpl{
	private static final String URL="http://vip.book.sina.com.cn/book_lib.php?pub=2&s=list&order=rdc&dpc=1";
	private static final String	SIGN="书库大全热门排行";
	private static final String	TITLE="读书排行";
	private static final String URL_BASE="http://vip.book.sina.com.cn";
	public SinaBookParser() {
		super(TITLE, TOP10Global.TYPE_Reading, URL);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		
		return null;
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		//System.out.println(body.getTextContent());
		Node node = getNodeByText(body,"a",SIGN);
		
		node=node.getParentNode().getNextSibling().getNextSibling();
		ArrayList<Node> dls = new ArrayList<Node>();
		getAllTagsByParent(node, "a", dls);
		List<Reading> list = new ArrayList<Reading>();
		for(int i=0;i<10;i++){
			Node a=dls.get(i);
			Reading r=new Reading();
			r.setLinkUrl(URL_BASE+getAttrValueByTag(a, "href"));
			r.setName(a.getTextContent());
			r.setRank(i+1);
			System.out.println(r);
			list.add(r);
		}
		return list;
	}
	
}

class SinaEmotionParser extends TOPParserImpl{
	private static final String URL="http://eladies.sina.com.cn/eladies/rank/index.shtml";
	private static final String	SIGN="wzcList";
	private static final String	TITLE="情感";
	public SinaEmotionParser() {
		super(TITLE, TOP10Global.TYPE_Emotion, URL);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		return null;
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("tbody","id",SIGN,body);
		
		ArrayList<Node> dls = new ArrayList<Node>();
		getAllTagsByParent(node, "a", dls);
		List<Emotion> list = new ArrayList<Emotion>();
		int rank=1;
		for(int i=0;i<20;i+=2){
			Node a=dls.get(i);
			Emotion r=new Emotion();
			r.setLinkUrl(getAttrValueByTag(a, "href"));
			r.setName(a.getTextContent());
			r.setRank(rank++);
			System.out.println(r);
			list.add(r);
		}
		return list;
	}
	
}
